/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
        int len1 = 0;
        int len2 = 0;
        ListNode* p1 = pHead1;
        ListNode* p2 = pHead2;
        while (p1)
        {
            p1 = p1->next;
            len1++;
        }
        while (p2)
        {
            p2 = p2->next;
            len2++;
        }
        if (len1 > len2)
        {
            int i = len1 - len2;
            while (i--)
            {
                pHead1 = pHead1->next;
            }
        }
        else
        {
            int i = len2 - len1;
            while (i--)
            {
                pHead2 = pHead2->next;
            }
        }
        while (pHead1 != pHead2)
        {
            pHead1 = pHead1->next;
            pHead2 = pHead2->next;
        }
        return pHead1;
    }
};